package com.tim.app.ui.fragment.selceted;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.User;
import com.tim.app.ui.adapter.selceted.AtAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @列表页面
 */
public class AtFragment extends BaseFragment implements View.OnClickListener, LoadMoreHandler, BaseRecyclerAdapter.OnItemClickListener {

    public static final String TAG = "AtFragment";
    private View rootView;

    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvFriend;
    private EmptyLayout emptyLayout;
    private AtAdapter adapter;
    private List<User> dataList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_at, container, false);

            load_more = (LoadMoreRecycleViewContainer) rootView.findViewById(R.id.load_more);
            wrvFriend = (WrapRecyclerView) rootView.findViewById(R.id.wrvFriend);

            wrvFriend.setOverScrollMode(View.OVER_SCROLL_NEVER);
            load_more.useDefaultFooter(View.GONE);
            load_more.setAutoLoadMore(true);
            load_more.setLoadMoreHandler(this);

            emptyLayout = new EmptyLayout(getActivity(), load_more);
//            emptyLayout.showLoading();
//            emptyLayout.setEmptyText(RT.getString(R.string.no_search_data));
            emptyLayout.setEmptyButtonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    emptyLayout.showLoading();
                }
            });

            if (dataList == null) {
                dataList = new ArrayList<>();
            }
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            wrvFriend.setLayoutManager(layoutManager);

            adapter = new AtAdapter(getActivity(), dataList);
            adapter.setOnItemClickListener(this);
            wrvFriend.setAdapter(adapter);
        }
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onItemClick(View view, int position, long id) {
        User user = dataList.get(position);
        EventManager.ins().sendEvent(EventTag.ON_AT, 0, 0, user);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView && null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


}
